package main;

//import java.applet.*;
//import java.awt.*;
//import java.awt.event.*;

import equipment.*;
import terrain.Terrain;
import unit.*;

import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Random;

public class Game { 
	
	public Board board;
	public Army[] armies = {null, null};
	public boolean armiesPlaced = false;
	public ArrayList<Battle> battles = new ArrayList<Battle>();
	public static final int GOODINDEX = 0;
	public static final int BADINDEX = 1;
	public static String[] armyTitles = {"Good", "Bad"};
	
	public Area goodStart = null;
	public Area badStart = null;
		
	public int priority = 0;
	public int currArmy = 0;
	public String currPhase = "Move";
	public int turnNumber = 0;
	
	public static Random rng = new Random();
	
	public UI myUI;
	
	public Game() {
		Board b = new Board(24, 24);
		this.board = b;
		b.game = this;
		
		Unit h1 = new HighElf(1,0,50,50);
		h1.equip(new ElfBow());
		h1.equip(new ElvenBlade());
		h1.equip(new Shield());
		Unit h2 = new HighElf(2,0,100,50);
		h2.equip(new ElfBow());
		
		Unit g1 = new Goblin(11,1,50,150);
		g1.equip(new OrcBow());
		Unit g2 = new Goblin(12,1,100,150);
		g2.equip(new Shield());
		Unit g3 = new Goblin(13,1,150,150);
		g3.equip(new Shield());
		g3.equip(new Spear());
		
		ArrayList<Unit> a1 = new ArrayList<Unit>();
		a1.add(h1);
		a1.add(h2);
		Army goodArmy = new Army(a1, this);
		
		ArrayList<Unit> a2 = new ArrayList<Unit>();
		a2.add(g1);
		a2.add(g2);
		a2.add(g3);
		Army badArmy = new Army(a2, this);
		
		armies[0] = goodArmy;
		armies[1] = badArmy;
		battles = new ArrayList<Battle>();
		priority = 0;
		currArmy = 0;
		currPhase = "Move";
	}
	
	public void setupTest() {
		
	}
	
	public void endPhase() {
		myUI.message = "";
		if (currPhase == "Fight") {
			if (battles.size() > 0) {
				myUI.message = "Still battles to fight";
				myUI.repaint();
				return;
			}
			
			int winner = this.winConditions();
			if (winner == 0) {
				UI.messageLog.add("Good side wins!");
				myUI.endGame();
				myUI.repaint();
				return;
			}
			else if (winner == 1) {
				UI.messageLog.add("Bad side wins!");
				myUI.endGame();
				myUI.repaint();
				return;
			}
			else if (winner == 2) {
				UI.messageLog.add("It's a draw!");
				myUI.endGame();
				myUI.repaint();
				return;
			}
			
			// Calculate priority
			int priorityGood = rng.nextInt(6)+1;
			int priorityBad = rng.nextInt(6)+1;
			UI.messageLog.add("Priority rolls: Good-"+priorityGood+" Bad-" + priorityBad);
			if (priorityGood > priorityBad || (priorityGood == priorityBad && priority == 1)){
				priority = 0;
				UI.messageLog.add("Good has priority");
			}		
			else if (priorityBad > priorityGood){
				priority = 1;
				UI.messageLog.add("Bad has priority");
			}
			
			// Refresh moves
			for (Army a : armies) {
				for (Unit u : a.units) {
					u.refresh();
				}
			}
			turnNumber++;
			currArmy = priority;
			
			currPhase = "Move";
		}
		else if (priority == currArmy) {
			currArmy = (currArmy+1)%2;
		}
		else {
			currArmy = priority;
			if (currPhase == "Move") {
				currPhase = "Shoot";
			}
			else if (currPhase == "Shoot") {
				for (Army a : this.armies) {
					for (Unit u : a.units){
						u.moveLeft = 0;
					}
				}
				currPhase = "Fight";
			}
		}
		myUI.repaint();
	}

	public int winConditions() {
		if (armies[0].units.size() < armies[0].origSize/4) {
			if (armies[1].units.size() < armies[1].origSize/4)
				return 2;
			else
				return 1;
		}
		if (armies[1].units.size() < armies[1].origSize/4)
			return 0;
		return -1;
	}
	
	public boolean canPlace(Unit unit, int x, int y) {
		if (UI.DEBUG)
			System.out.println("Checking unit " + unit.toChoiceString() + " currently at: (" + unit.x + ", " + unit.y + "), moving to (" + x + ", " + y + ")");
		// Check for intersection with all other units
		for (Army a : armies) {
			for (Unit u : a.units) {
				if (Utils.pixelDist(x,y, u.x,u.y) < unit.pixelRadius+u.pixelRadius-1 && u != unit) 
					return false;
			}
		}
		Area unitArea = new Area(new Ellipse2D.Double(x-unit.pixelRadius+1, y-unit.pixelRadius+1, 
				2*unit.pixelRadius-2, 2*unit.pixelRadius-2));
		for (Terrain t : board.terrainList) {
			Area clone = (Area) unitArea.clone();
			clone.intersect(t.obstacle);
			if (!clone.isEmpty()){
				System.out.println("Intersect with terrain" + t);
				return false;
			}
		}
		return true;
	}
	
	public void printInfo() {
		System.out.println("PRINTING GAME INFO");
		System.out.println("Turn number: " + this.turnNumber);
		System.out.println("Priority: " + this.priority);
		System.out.println("currPhase: " + this.currPhase);
		System.out.println("currArmy: " + this.currArmy);
		System.out.println("------------------");
		System.out.println("Armies: " + this.armies);
		System.out.println("-------------");
		System.out.println("Units (Good):");
		System.out.println("-------------");
		for (Unit u : this.armies[GOODINDEX].units)
			u.printInfo();
		System.out.println("------------");
		System.out.println("Units (Bad):");
		System.out.println("------------");
		for (Unit u : this.armies[BADINDEX].units)
			u.printInfo();
		System.out.println("-------------");
		System.out.println("Battles :");
		System.out.println("-------------");
		System.out.println(this.battles);
		System.out.println("-------------");
		System.out.println("Message Log: ");
		System.out.println("-------------");
		for(String s : UI.messageLog){
			System.out.println(s);
		}
	}
}